package com.kgc.ssm.service.impl;

import com.kgc.ssm.entity.SysUsers;
import com.kgc.ssm.entity.SysUsersExample;
import com.kgc.ssm.mapper.SysUsersMapper;
import com.kgc.ssm.service.SysUsersService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class SysUsersServiceImpl implements SysUsersService {
    @Resource
    private SysUsersMapper sysUsersMapper;

    @Override
    public void register(SysUsers sysUsers) {
        //散列次数
        int hashIterations = 2;
        //对密码进行加密
        Md5Hash md5Hash = new Md5Hash(sysUsers.getPassword(), "Zy,.", hashIterations);
        //设置用户的密码为密文
        sysUsers.setPassword(md5Hash.toHex());
        //执行添加
        sysUsersMapper.insertSelective(sysUsers);
    }

    @Override
    public SysUsers findByLoginId(String loginId) {
        SysUsersExample example = new SysUsersExample();
        example.createCriteria().andLoginIdEqualTo(loginId);
        List<SysUsers> list = sysUsersMapper.selectByExample(example);
        return ObjectUtils.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public List<String> findPermissionByName(String name) {
        return sysUsersMapper.findPermissionByName(name);
    }

    @Override
    public List<String> findRolesByName(String name) {
        return sysUsersMapper.findRolesByName(name);
    }
}
